/**
 * Created by myself on 2016/6/27.
 */

//删除
function deleteObj(objid)
{

    bootbox= bootbox_global;
    // alert(roleid);

    bootbox_global.dialog({
        message: "确定要删除吗?",
        buttons: {
            Cancel: {
                label: "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                callback: function () {
                }
            }
            , OK: {
                label: "<i class='icon-ok'></i> 确定",
                "className": "btn-sm btn-success",
                callback: function () {


                    //发送请求
                    send_request_direct("/ucPreviewList/Delete", "obj_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });

                }
            }
        }
    });

}

//修改对象
function updateObj(objid,datarow)
{

    bootbox= bootbox_global;

    var obj = grid.date_list[datarow];
    var designpicarray = obj.pic_url.split(",");




    var alertarray = {
        form: {api: "/ucPreviewList/Update", name: "alertform", id: "alertform"},
        inputs: [

            {title: "名称", type: "text", placeholder: "", verify: "empty", name: "name", id: "name",value:obj.name},

            //{title: "效果图", type: "img", placeholder: "", verify: "empty", name: "pic_url", id: "upimgs",src:obj.pic_url},

            {title: "效果图1", type: "img", placeholder: "", verify: "empty", name: "design1", id: "design1",src:getArrayVal(designpicarray,0)},

            {title: "浏览次数", type: "text", placeholder: "", verify: "empty", name: "view_count", id: "view_count",value:obj.view_count},
            {title: "订单数量", type: "text", placeholder: "", verify: "empty", name: "order_count", id: "order_count",value:obj.order_count},
            {title: "工人ID", type: "select", id: "roles2", form_name: "worker_id", copyid: "typeselect2",selectvalue:obj.worker_id},
            {title: "种类", type: "select", id: "roles", form_name: "type_id", copyid: "typeselect",selectvalue:obj.type_id},

            {title: "效果图2", type: "img", placeholder: "", verify: "", name: "design2", id: "design2",src:getArrayVal(designpicarray,1)},
            {title: "效果图3", type: "img", placeholder: "", verify: "", name: "design3", id: "design3",src:getArrayVal(designpicarray,2)},
            {title: "效果图4", type: "img", placeholder: "", verify: "", name: "design4", id: "design4",src:getArrayVal(designpicarray,3)},
            {title: "效果图5", type: "img", placeholder: "", verify: "", name: "design5", id: "design5",src:getArrayVal(designpicarray,4)},
            {title: "效果图6", type: "img", placeholder: "", verify: "", name: "design6", id: "design6",src:getArrayVal(designpicarray,5)},

        ]

    }

    bootbox.dialog({
        title: "修改信息",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&obj_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });

}





var bootbox_global;
var grid;
define([
    "jquery",
    "IO",
    "bootbox",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
], function ($, IO, bootbox) {

    bootbox_global = bootbox;


    $('#fileupload').fileupload({
        url: '/uploadPreview',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {

        var imgid = $(e.target).attr('imgid');
        document.getElementById(imgid).src = laoding_gif;

    }).on('fileuploadprocessalways', function (e, data) {
        //console.log("fileuploadprocessalways",data);
    }).on('fileuploadprogressall', function (e, data) {
        //console.log("fileuploadprogressall",data);
    }).on('fileuploaddone', function (e, data) {

        console.log("upyuntype " + $('#fileupload').attr("upyuntype"));
        var imgid = $(e.target).attr('imgid');
        document.getElementById(imgid).src = data._response.result.url;
    });


    $(function () {
        var columns_select ={name_dataIndex :'name',value_dataIndex :'id'};
        var  grid_select = new Select_Grid("/ucPreivew/getListPaging",$("#typeselect"),columns_select,"rows=100000");

        grid_select.removeflag = 1;
        grid_select.Init(function(){

            dataInit();


        });

        var columns_select2 ={name_dataIndex :'real_name',value_dataIndex :'worker_id'};
        var  grid_select2 = new Select_Grid("/worker/getDesignerListPaging",$("#typeselect2"),columns_select2,"rows=100000&type_id=1");

        grid_select2.removeflag = 1;
        grid_select2.Init(function(){

            dataInit();


        });

        //每列数据列性
        var columns = [

            {
                title: '编号', dataIndex: 'ID', dataSource: function (data, datarow, gridobj, current_column) {

                return "<span>" + parseInt((gridobj.current_page - 1) * gridobj.page_rows + parseInt(datarow) + 1) + "</span>";
            }
            },

            {title: '名称', dataIndex: 'name',},

            {
                title: '工人', dataIndex: 'gender', dataSource: function (data, datarow, gridobj, current_column) {

                var obj = data[datarow];

                return obj.worker.real_name;


            }
            },

            { title: '效果图列表', dataIndex: 'avatar',dataSource:function(data,datarow,gridobj,current_column) {

                var designpicarray = data[datarow].pic_url.split(",");

                var html ="";

                for(var i=0;i<designpicarray.length;i++)
                {

                    html +="<img class='girdimgthunb' onclick=\"viewUrl('"+designpicarray[i]+"')\" src='"+designpicarray[i]+"'/>&nbsp;";

                }

                return html;

            }},
            { title: '浏览次数', dataIndex: 'view_count',},
            { title: '订单数量', dataIndex: 'order_count',},
            { title: '提交时间', dataIndex: 'create_time',},
            { title: '最后更新时间', dataIndex: 'update_time',},
            {title : '操作',dataIndex :'ID',dataSource:function(data,datarow,gridobj,current_column) {

                var html= "<div class='div_btn'><button onclick=\"updateObj("+data[datarow].id+","+datarow+")\" class='btn btn-info'>修改</button> <button onclick=\"deleteObj("+data[datarow].id+")\"  class='btn btn-danger'>删除</button>";




                html = html+"</div>"

                return html;
            }},


        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/ucPreviewList/getListPaging", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param = "type_id=" + $("#typeselect").val();

            return param;

        }

        //dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });

        //新增
        $("#btn_add").bind("click", function () {


            var alertarray = {
                form: {api: "/ucPreviewList/add", name: "alertform", id: "alertform"},
                inputs: [

                    {title: "名称", type: "text", placeholder: "", verify: "empty", name: "name", id: "name"},

                    //{title: "效果图", type: "img", placeholder: "", verify: "empty", name: "pic_url", id: "upimgs",src:""},
                    {title: "效果图1", type: "img", placeholder: "", verify: "empty", name: "design1", id: "design1",src:""},

                    {title: '浏览次数', type: "text", placeholder: "", verify: "empty", name: "view_count", id: "view_count"},
                    {title: '订单数量', type: "text", placeholder: "", verify: "empty", name: "order_count", id: "order_count"},
                    {title: "工人ID", type: "select", id: "roles2", form_name: "worker_id", copyid: "typeselect2"},
                    {title: "种类", type: "select", id: "roles", form_name: "type_id", copyid: "typeselect"},

                    {title: "效果图2", type: "img", placeholder: "", verify: "", name: "design2", id: "design2",src:""},
                    {title: "效果图3", type: "img", placeholder: "", verify: "", name: "design3", id: "design3",src:""},
                    {title: "效果图4", type: "img", placeholder: "", verify: "", name: "design4", id: "design4",src:""},
                    {title: "效果图5", type: "img", placeholder: "", verify: "", name: "design5", id: "design5",src:""},
                    {title: "效果图6", type: "img", placeholder: "", verify: "", name: "design6", id: "design6",src:""},

                ]

            }
            bootbox.dialog({
                title: "新增",
                message: generateAlertForm(alertarray),
                buttons: {
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-danger",
                        "callback": function () {
                        }
                    },
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-success",
                        "callback": function () {

                            //验证
                            if (check_form_info_all(alertarray.form.name) == false)
                                return false;

                            //发送请求
                            ajax_request(alertarray.form.name, "&parent_id=-1", function process_data(data) {


                                //操作成功
                                AlertSuccess(bootbox, "操作成功", function () {

                                    grid.Init();


                                });


                            }, function process_fail(data) {

                                //操作失败
                                AlertError(bootbox, data.msg);


                            });


                        }
                    }
                }
            });


        });
    });


});
